package com.sojson.config.swagger;

import static springfox.documentation.schema.Annotations.findPropertyAnnotation;
import static springfox.documentation.swagger.schema.ApiModelProperties.findApiModePropertyAnnotation;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;

import io.swagger.annotations.ApiModelProperty;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

/**
 * Swagger字段排序
 * 
 * @author liu
 * @date 2021-05-17
 */
@Component
public class CustomApiModelPropertyPositionBuilder implements ModelPropertyBuilderPlugin {

    /** 不参与排序的字段集合 */
    private List<String> fieldNamesNo = new ArrayList<>();

    {
        fieldNamesNo.add("id");
        fieldNamesNo.add("pageNum");
        fieldNamesNo.add("pageSize");
        fieldNamesNo.add("orderByColumn");
    }

    @Override
    public void apply(ModelPropertyContext context) {
        Optional<BeanPropertyDefinition> beanPropertyDefinitionOpt = context.getBeanPropertyDefinition();
        if (!beanPropertyDefinitionOpt.isPresent()) {
            return;
        }
        BeanPropertyDefinition beanPropertyDefinition = beanPropertyDefinitionOpt.get();
        AnnotatedField field = beanPropertyDefinition.getField();
        if (field == null) {
            return;
        }
        Class<?> clazz = field.getDeclaringClass();
        Optional<ApiModelProperty> annotation = Optional.absent();
        Optional<AnnotatedElement> annotatedElement = context.getAnnotatedElement();
        if (annotatedElement.isPresent()) {
            annotation = annotation.or(findApiModePropertyAnnotation(annotatedElement.get()));
        }

        boolean present = beanPropertyDefinitionOpt.isPresent();
        if (present) {
            annotation = annotation.or(findPropertyAnnotation(beanPropertyDefinitionOpt.get(), ApiModelProperty.class));
        }

        if (present) {
            if (annotation.isPresent() && annotation.get().position() != 0) {
                return;
            }
            clazz = field.getDeclaringClass();
            Field[] declaredFields = clazz.getDeclaredFields();
            Field declaredField;
            try {
                declaredField = clazz.getDeclaredField(field.getName());
            } catch (NoSuchFieldException | SecurityException e) {
                e.printStackTrace();
                return;
            }
            int indexOf = ArrayUtils.indexOf(declaredFields, declaredField);
            if (indexOf != -1 && !fieldNamesNo.contains(field.getName())) {
                ApiModelProperty annotation2 = declaredField.getAnnotation(ApiModelProperty.class);
                if (annotation2 != null) {
                    context.getBuilder().position(indexOf + 1);
                }
            }
        }
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return SwaggerPluginSupport.pluginDoesApply(delimiter);
    }

}